package cn.openea.perpartner.common.core.web.page;


import cn.openea.perpartner.common.core.text.Convert;
import cn.openea.perpartner.common.core.utils.ServletUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * 表格数据处理
 */
@Slf4j
public class TablePage {
    private static final String CONTENT_TYPE = "application/json";
    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain() {
        PageDomain pageDomain = new PageDomain();
        String method = ServletUtils.getRequest().getMethod();
        // get 请求没有有请求体
        if(ServletUtils.getRequest().getContentType().toLowerCase().contains(CONTENT_TYPE) && !"GET".equalsIgnoreCase(method)){
            pageDomain.setPageNum(Convert.toInt(ServletUtils.getJsonParameter("pageNum"),1));
            pageDomain.setPageSize(Convert.toInt(ServletUtils.getJsonParameter("pageSize"),10));
        }else {
            pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameter("pageNum"),1));
            pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter("pageSize"),10));
        }
        pageDomain.setOrderByColumn(ServletUtils.getParameter("orderByColumn"));
        pageDomain.setIsAsc(ServletUtils.getParameter("isAsc"));
        return pageDomain;
    }

    public static PageDomain buildPageRequest() {
        return getPageDomain();
    }

    public static PageDomain buildMinRestPageRequest(JSONObject formData) {
        return getMinRestPageDomain(formData);
    }

    public static PageDomain getMinRestPageDomain(JSONObject formData) {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(Convert.toInt(formData.getString("pageNum")));
        pageDomain.setPageSize(Convert.toInt(formData.getString("pageSize")));
        pageDomain.setOrderByColumn(formData.getString("orderByColumn"));
        pageDomain.setIsAsc(formData.getString("isAsc"));
        return pageDomain;
    }
}
